package com.guli.gulimall.product.service.impl;



import com.guli.common.utils.R;
import com.guli.common.vo.Attr;
import com.guli.common.vo.SkuEsModel;
import com.guli.gulimall.product.dao.*;
import com.guli.gulimall.product.entity.*;
import com.guli.gulimall.product.feign.CouponFeignService;
import com.guli.gulimall.product.filter.FiliterEn;
import com.guli.gulimall.product.service.*;
import com.guli.gulimall.product.to.MemberPriceTo;
import com.guli.gulimall.product.to.SkuFullReductionTo;
import com.guli.gulimall.product.to.SkuLadderTo;
import com.guli.gulimall.product.to.SpuBoundsTo;

import com.guli.gulimall.product.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import org.springframework.transaction.annotation.Transactional;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    SpuInfoDescDao spuInfoDescDao;

    @Autowired
    SpuImagesService spuImagesService;

    @Autowired
    AttrDao attrDao;

    @Autowired
    ProductAttrValueService productAttrValueService;

    @Autowired
    SkuInfoDao skuInfoDao;

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    CouponFeignService couponFeignService;


    @Autowired
    SpuInfoDao spuInfoDao;

    @Autowired
    ProductAttrValueDao productAttrValueDao;

    @Autowired
    private  SkuSaleAttrValueDao skuSaleAttrValueDao;

    @Autowired
    private FiliterEn filiterEn;


    @Transactional
    @Override
    public void addup(SpuInfoVo spuInfoVo) {

//        ----------pms数据库-------------
//        1. pms_spu_info spu的基本信息
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(spuInfoVo,spuInfoEntity);

        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        baseMapper.insert(spuInfoEntity);
//        2. pms_spu_info_desc 商品的介绍图集表
        List<String> images = spuInfoVo.getImages();
        if (images!=null){
            String imgs="";
            for (String url : images) {
                imgs+=","+url;
            }
            imgs = imgs.substring(1);
            SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
            descEntity.setDecript(imgs);
            descEntity.setSpuId(spuInfoEntity.getId());
            spuInfoDescDao.insert(descEntity);
        }
//        3. pms_spu_images 商品的左上角小图
        List<String> images1 = spuInfoVo.getImages();
        if (images1!=null){
            List<SpuImagesEntity> collect = images1.stream().map(url -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                spuImagesEntity.setSpuId(spuInfoEntity.getId());
                spuImagesEntity.setImgUrl(url);
                return spuImagesEntity;
            }).collect(Collectors.toList());
            spuImagesService.saveBatch(collect);
        }
//        4. pms_product_attr_value 页面中做添加的所有的规格参数信息
        List<BaseAttrs> baseAttrs = spuInfoVo.getBaseAttrs();
        if (baseAttrs!=null){
            List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attr -> {
                ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                productAttrValueEntity.setSpuId(spuInfoEntity.getId());
                productAttrValueEntity.setAttrId(attr.getAttrId());
                productAttrValueEntity.setAttrValue(attr.getAttrValues());

                AttrEntity attrEntity = attrDao.selectById(attr.getAttrId());
                productAttrValueEntity.setAttrName(attrEntity.getAttrName());
                productAttrValueEntity.setQuickShow(attr.getShowDesc());
                productAttrValueEntity.setSearchType(attrEntity.getSearchType());
                return productAttrValueEntity;
            }).collect(Collectors.toList());
            productAttrValueService.saveBatch(collect);
        }

        // 9. sms_spu_bounds  spu的积分表
        if (spuInfoVo.getBounds()!=null){
            SpuBoundsTo spuBoundsTo = new SpuBoundsTo();
            spuBoundsTo.setSpuId(spuInfoEntity.getId());
            spuBoundsTo.setBuyBounds(spuInfoVo.getBounds().getBuyBounds());
            spuBoundsTo.setGrowBounds(spuInfoVo.getBounds().getGrowBounds());
            couponFeignService.saveSpuBounds(spuBoundsTo);


        }
        // 5. pms_sku_info 商品sku的基本信息
        List<Skus> skus = spuInfoVo.getSkus();
        if (skus!=null){
            for (Skus sku : skus) {
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(sku,skuInfoEntity);
                skuInfoEntity.setSaleCount(0l);
                List<Images> images2 = sku.getImages();
                if (images2!=null){
                    Images images3 = images2.stream().filter(item -> {

                        return item.getDefaultImg().equals(1);
                    }).findFirst().get();


                    skuInfoEntity.setSkuDefaultImg(images3.getImgUrl());
                }
                skuInfoEntity.setSpuId(spuInfoEntity.getId());
                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
                skuInfoEntity.setSkuDesc(spuInfoEntity.getSpuDescription());

                skuInfoDao.insert(skuInfoEntity);


// 6. pms_sku_images 商品sku的图片（一个sku多个图片

                List<Images> images3 = sku.getImages();

                if (images3!=null){

                    List<SkuImagesEntity> collect = images3.stream().filter(item -> {

                        return StringUtils.isNotEmpty(item.getImgUrl());
                    }).map(item -> {
                        SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                        skuImagesEntity.setSkuId(skuInfoEntity.getSkuId());
                        skuImagesEntity.setImgUrl(item.getImgUrl());
                        skuImagesEntity.setDefaultImg(item.getDefaultImg());
                        return skuImagesEntity;
                    }).collect(Collectors.toList());

                    skuImagesService.saveBatch(collect);
                }
                // 7. pms_sku_sale_attr_value sku的销售属性表
                List<Attr> attrs = sku.getAttr();
                if(attrs!=null){
                    List<SkuSaleAttrValueEntity> collect = attrs.stream().map(attr -> {
                        SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                        BeanUtils.copyProperties(attr, skuSaleAttrValueEntity);
                        AttrEntity attrEntity = attrDao.selectById(attr.getAttrId());
                        skuSaleAttrValueEntity.setSearchType(attrEntity.getSearchType());
                        skuSaleAttrValueEntity.setSkuId(skuInfoEntity.getSkuId());
                        return skuSaleAttrValueEntity;
                    }).collect(Collectors.toList());
                    skuSaleAttrValueService.saveBatch(collect);
                }




                // --------------优惠券数据----------------
                //8. sms_sku_ladder  sku的打折信息表
                SkuLadderTo skuLadderTo = new SkuLadderTo();

                skuLadderTo.setSkuId(skuInfoEntity.getSkuId());
                skuLadderTo.setAddOther(sku.getCountStatus());
                skuLadderTo.setFullCount(sku.getFullCount());
                skuLadderTo.setDiscount(sku.getDiscount());
                R r1 = couponFeignService.saveSkuLadder(skuLadderTo);


                // 10. sms_sku_full_reduction  sku的满减

                SkuFullReductionTo skuFullReductionTo = new SkuFullReductionTo();

                skuFullReductionTo.setAddOther(sku.getPriceStatus());
                skuFullReductionTo.setFullPrice(sku.getFullPrice());
                skuFullReductionTo.setReducePrice(sku.getReducePrice());
                skuFullReductionTo.setSkuId(skuInfoEntity.getSkuId());

                R r = couponFeignService.saveSkuFullReduction(skuFullReductionTo);


                // 11. sms_member_price  会员等级价格表

                List<MemberPrice> memberPrice = sku.getMemberPrice();
                if (memberPrice!=null){
                    List<MemberPriceTo> collect = memberPrice.stream().map(item -> {
                        MemberPriceTo memberPriceTo = new MemberPriceTo();
                        memberPriceTo.setSkuId(skuInfoEntity.getSkuId());
                        memberPriceTo.setMemberLevelId(item.getId());
                        memberPriceTo.setMemberLevelName(item.getName());
                        memberPriceTo.setMemberPrice(item.getPrice());

                        return memberPriceTo;
                    }).collect(Collectors.toList());
                    R r2 = couponFeignService.saveMemberPrice(collect);
                }

            }
        }


    }

    @Override
    public R upAll(Long spuid) throws IOException {
     List<Attr> spuAttrs= productAttrValueDao.getSpuAttrValues(spuid);

      List<SkuEsModel> skuEsModelList= spuInfoDao.getSkuEsModel(spuid);

        for (SkuEsModel skuEsModel : skuEsModelList) {

         Set<Attr> skuAttrs=  skuSaleAttrValueDao.getSkuAttrVaulues(skuEsModel.getSkuId());

         skuAttrs.addAll(spuAttrs);
         skuEsModel.setAttrs(new ArrayList<>(skuAttrs));

        }

        R r = filiterEn.upSku(skuEsModelList);

        return r;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }
}